package com.zyy.study.always.code3;

import java.io.*;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Set;
import java.util.UUID;

public class BankSystem implements Serializable{
    private HashMap<String, Account> accounts;

    public boolean createAccount(String name) {
        String id = "ACC" + UUID.randomUUID().toString().replace("-", "");
        Account account = new Account(id, name, 0, LocalDate.now(), 0, 0);
        if (accounts.containsKey(id)) {
            return false;
        }
        accounts.put(id, account);
        return true;
    }

    /**
     * 存款
     *
     * @param id
     * @param money
     */
    public void deposit(String id, double money) {
        if (!accounts.containsKey(id)) {
            System.out.println("无此账户");
            return;
        }
        Account account = accounts.get(id);
        double oldMoney = account.getMoney();
        if (validateDepositMoney(account, money)) {
            account.setMoney(oldMoney + money);
            updateDepositMoney(account, money);
        }

    }

    /**
     * 取款
     *
     * @param id
     * @param money
     */
    public void withdraw(String id, double money) {
        if (!accounts.containsKey(id)) {
            System.out.println("无此账户");
            return;
        }
        Account account = accounts.get(id);
        double oldMoney = account.getMoney();
        if (validatewithdrawMoney(account, money)) {
            account.setMoney(oldMoney - money);
            updateWithdrawMoney(account,money);
        }

    }

    public void updateDepositMoney(Account account,double money) {
        account.setDepositMoney(account.getDepositMoney() + money);
    }
    public void updateWithdrawMoney(Account account,double money) {
        account.setWithdrawMoney(account.getWithdrawMoney() + money);
    }

    public boolean validateDepositMoney(Account account, double money) {
        double depositMoney = account.getDepositMoney();
        if (money > 10000) {
            return false;
        }
        if (!account.getDate().equals(LocalDate.now())){
            account.setDate(LocalDate.now());
            account.setMoney(0);
        }
        if (depositMoney + money > 50000) {
            return false;
        }
        return true;
    }
    public boolean validatewithdrawMoney(Account account, double money) {
        double withdrawMoney = account.getWithdrawMoney();
        if (money > 10000) {
            return false;
        }
        if (!account.getDate().equals(LocalDate.now())){
            account.setDate(LocalDate.now());
            account.setMoney(0);
        }
        if (withdrawMoney + money > 50000) {
            return false;
        }
        return true;
    }

    /**
     * 转账
     *
     * @param id1
     * @param id2
     * @param money
     * @return
     */
    public boolean transfer(String id1, String id2, double money) {
        if (!(accounts.containsKey(id1) && accounts.containsKey(id2))) {
            System.out.println("无账户");
            return false;
        }
        try {
            Account fromAccount = accounts.get(id1);
            Account toAccount = accounts.get(id2);
            double fromMoney = fromAccount.getMoney();
            double toMoney = toAccount.getMoney();
            if (validateDepositMoney(fromAccount, fromMoney) && validatewithdrawMoney(toAccount, toMoney)) {
                fromAccount.setMoney(fromMoney - money);
                toAccount.setMoney(toMoney + money);
                updateDepositMoney(fromAccount, money);
                updateWithdrawMoney(toAccount, money);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
        return true;


    }

    public void saveToFile(String filePath) {
        try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(filePath))) {
            objectOutputStream.writeObject(accounts);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void loadFromFile(String filePath) {
        byte[] bytes = new byte[1024];
        try (ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(filePath))) {
            HashMap<String, Account> accountHashMap = (HashMap<String, Account>)objectInputStream.readObject();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
